package com.assignment.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import com.assignment.database.DBOperations;
import com.assignment.database.DatabaseHandler;
import com.assignment.main.Item;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class ViewItemScreen extends JFrame {

	private JPanel contentPane;
	private JLabel itemDescLabel;
	private JLabel imageLabel;
	private JLabel itemTypeLabel;
	private JLabel buyPriceLabel;
	private JLabel sellPriceLabel;
	private JLabel stockQuantityLabel,lblItemName;
	private JLabel styleLabel;

	Item item;
	JFrame thisFrame = this;
	final SearchScreen screen;
	
	/**
	 * @wbp.parser.constructor
	 */
	public ViewItemScreen(Item item, SearchScreen screen) {
		this(screen);
		this.item=item;
		
		itemTypeLabel.setText(item.typeNames[item.getType()]);
		lblItemName.setText(item.getName());
		itemDescLabel.setText(item.getDescription());
		buyPriceLabel.setText(String.valueOf(item.getBuyPrice()));
		sellPriceLabel.setText(String.valueOf(item.getSellPrice()));
		stockQuantityLabel.setText(String.valueOf(item.getStock()));
		styleLabel.setText(String.valueOf(item.getStyle()));
		
		
		try {
			Image i =  DBOperations.getImage(item.getId()).getImage().getScaledInstance(200, 200, 0);
	        
	        ImageIcon ii = new ImageIcon(i);
	        
	        imageLabel.setIcon(ii);
	        
			}catch (Exception e) {System.out.println("Error loading image");};
		
	}
	public ViewItemScreen(final SearchScreen screen) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 534, 361);
		this.screen=screen;
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel.setBounds(205, 11, 115, 47);
		contentPane.add(panel);
		panel.setLayout(null);
		
		 lblItemName = new JLabel("       Item Name");
		lblItemName.setBounds(10, 11, 96, 25);
		panel.add(lblItemName);
		
		JPanel panel_1 = new JPanel();
		
		panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_1.setBounds(10, 82, 233, 233);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		imageLabel = new JLabel("");
		imageLabel.setBounds(10, 10, 210, 210);
		panel_1.add(imageLabel);
		imageLabel.setIcon(new ImageIcon());
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_2.setBounds(253, 82, 248, 233);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		JLabel lblItemDecription = new JLabel("Item Decription:");
		lblItemDecription.setBounds(10, 11, 93, 29);
		panel_2.add(lblItemDecription);
		
		JLabel lblNewLabel = new JLabel("Item Type:");
		lblNewLabel.setBounds(10, 39, 93, 21);
		panel_2.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Buy Price:");
		lblNewLabel_1.setBounds(10, 71, 93, 14);
		panel_2.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Sell Price:");
		lblNewLabel_2.setBounds(10, 96, 80, 14);
		panel_2.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Style");
		lblNewLabel_3.setBounds(10, 149, 64, 14);
		panel_2.add(lblNewLabel_3);
		
		itemDescLabel = new JLabel("EXAMPLE");
		itemDescLabel.setBounds(113, 18, 125, 14);
		panel_2.add(itemDescLabel);
		
		JLabel lblStockQuantity = new JLabel("Stock Quantity");
		lblStockQuantity.setBounds(10, 124, 93, 14);
		panel_2.add(lblStockQuantity);
		
		itemTypeLabel = new JLabel("EXAMPLE");
		itemTypeLabel.setBounds(113, 42, 125, 14);
		panel_2.add(itemTypeLabel);
		
		buyPriceLabel = new JLabel("EXAMPLE");
		buyPriceLabel.setBounds(113, 71, 125, 14);
		panel_2.add(buyPriceLabel);
		
		sellPriceLabel = new JLabel("EXAMPLE");
		sellPriceLabel.setBounds(113, 96, 125, 14);
		panel_2.add(sellPriceLabel);
		
		stockQuantityLabel = new JLabel("EXAMPLE");
		stockQuantityLabel.setBounds(113, 124, 125, 14);
		panel_2.add(stockQuantityLabel);
		
		styleLabel = new JLabel("EXAMPLE");
		styleLabel.setBounds(113, 149, 125, 14);
		panel_2.add(styleLabel);
		
		JButton btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//open edit item screen
				
				new EditItemScreen(item, screen);
				
			}
		});
		btnEdit.setBounds(138, 186, 89, 23);
		panel_2.add(btnEdit);
		
		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				
			}
		});
		btnExit.setBounds(24, 186, 89, 23);
		panel_2.add(btnExit);
	}
}
